package chapter07

import scala.collection.mutable

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/11 11:55 AM
 */
object Test16_MergeMap {
  def main(args: Array[String]): Unit = {
    val map1 = Map(("a", 1), ("b", 3), "c" -> 6)
    val map2 = mutable.Map(("a", 6), ("b", 2), "c" -> 9, "d" -> 3)

    println(map1 ++ map2)
    println(map1.foldLeft(map2)(
      (mergeMap, kv) => {
        val key = kv._1
        val value = kv._2
        mergeMap(key) = mergeMap.getOrElse(key, 0) + value
        mergeMap
      }
    ))
  }
}
